﻿using RpcSync.Model;
using WeDonekRpc.CacheClient.Interface;

namespace RpcSync.Service.Accredit
{
    internal class DbCacheAccredit
    {
        private string _CacheKey;
        private readonly ICacheController _Cache;
        public string AccreditId { get; private set; }

        public DbCacheAccredit ( ICacheController cache )
        {
            this._Cache = cache;
        }

        protected void InitAccreditId ( string accreditId )
        {
            this.AccreditId = accreditId;
            this._CacheKey = "AccreditCache_" + accreditId;
        }
        protected void SetCache ( AccreditTokenDatum token )
        {
            DateTime time = token.OverTime;
            if ( time < DateTime.Now )
            {
                time = DateTime.Now.AddMinutes(1);
            }
            _ = this._Cache.Set(this._CacheKey, token, time);
        }
        protected void RmoveCache ()
        {
            _ = this._Cache.Remove(this._CacheKey);
        }
        protected bool TryGet ( out AccreditTokenDatum token )
        {
            return this._Cache.TryGet(this._CacheKey, out token);
        }
    }
}
